﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionCore;
using DLLPC.Utilities;

namespace COER.MotionUI
{
    public partial class UC_AixsHomeStatus : UserControl
    {
        public UC_AixsHomeStatus()
        {
            InitializeComponent();
        }

        public void Load()
        {
            timer1.Start();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            FreshStatus();
        }
        private void FreshStatus()
        {

            #region 门检状态
            btnDoor1.BackColor = Kernel.Exc.DI.X058_DoorMonitor1 == 1 ? Color.Green : Color.Red;
            btnDoor2.BackColor = Kernel.Exc.DI.X059_DoorMonitor2 == 1 ? Color.Green : Color.Red;
            #endregion
            
            btnCV1_TrackSW_Y.BackColor = AixsHomeStatus(Kernel.motion.CV1_TrackSW_Y.AxisMotionIO.SVON == 1, Kernel.motion.CV1_TrackSW_Y.IsHomeAlready, Kernel.motion.CV1_TrackSW_Y.axis.State == Advantech.Motion.AxisState.STA_AX_HOMING);
            btnCV2_TrackSW_Y.BackColor = AixsHomeStatus(Kernel.motion.CV2_TrackSW_Y.AxisMotionIO.SVON == 1, Kernel.motion.CV2_TrackSW_Y.IsHomeAlready, Kernel.motion.CV2_TrackSW_Y.axis.State == Advantech.Motion.AxisState.STA_AX_HOMING);
            btnCV1_Y.BackColor = AixsHomeStatus(Kernel.motion.CV1_Y.AxisMotionIO.SVON == 1, Kernel.motion.CV1_Y.IsHomeAlready, Kernel.motion.CV1_Y.axis.State == Advantech.Motion.AxisState.STA_AX_HOMING);
            btnCV2_Y.BackColor = AixsHomeStatus(Kernel.motion.CV2_Y.AxisMotionIO.SVON == 1, Kernel.motion.CV2_Y.IsHomeAlready, Kernel.motion.CV2_Y.axis.State == Advantech.Motion.AxisState.STA_AX_HOMING);
            btnCCD1_X.BackColor = AixsHomeStatus(Kernel.motion.CCD1_X.AxisMotionIO.SVON == 1, Kernel.motion.CCD1_X.IsHomeAlready, Kernel.motion.CCD1_X.axis.State == Advantech.Motion.AxisState.STA_AX_HOMING);
            btnCCD1_Y.BackColor = AixsHomeStatus(Kernel.motion.CCD1_Y.AxisMotionIO.SVON == 1, Kernel.motion.CCD1_Y.IsHomeAlready, Kernel.motion.CCD1_Y.axis.State == Advantech.Motion.AxisState.STA_AX_HOMING);
            btnCCD1_Z.BackColor = AixsHomeStatus(Kernel.motion.CCD1_Z.AxisMotionIO.SVON == 1, Kernel.motion.CCD1_Z.IsHomeAlready, Kernel.motion.CCD1_Z.axis.State == Advantech.Motion.AxisState.STA_AX_HOMING);
            btnCCD2_X.BackColor = AixsHomeStatus(Kernel.motion.CCD2_X.AxisMotionIO.SVON == 1, Kernel.motion.CCD2_X.IsHomeAlready, Kernel.motion.CCD2_X.axis.State == Advantech.Motion.AxisState.STA_AX_HOMING);
            btnCCD2_Y.BackColor = AixsHomeStatus(Kernel.motion.CCD2_Y.AxisMotionIO.SVON == 1, Kernel.motion.CCD2_Y.IsHomeAlready, Kernel.motion.CCD2_Y.axis.State == Advantech.Motion.AxisState.STA_AX_HOMING);
            btnCCD2_Z.BackColor = AixsHomeStatus(Kernel.motion.CCD2_Z.AxisMotionIO.SVON == 1, Kernel.motion.CCD2_Z.IsHomeAlready, Kernel.motion.CCD2_Z.axis.State == Advantech.Motion.AxisState.STA_AX_HOMING);
            btnInsp1_Y.BackColor = AixsHomeStatus(Kernel.motion.Insp1_Y.AxisMotionIO.SVON == 1, Kernel.motion.Insp1_Y.IsHomeAlready, Kernel.motion.Insp1_Y.axis.State == Advantech.Motion.AxisState.STA_AX_HOMING);
            btnInsp2_Y.BackColor = AixsHomeStatus(Kernel.motion.Insp2_Y.AxisMotionIO.SVON == 1, Kernel.motion.Insp2_Y.IsHomeAlready, Kernel.motion.Insp2_Y.axis.State == Advantech.Motion.AxisState.STA_AX_HOMING);
            btnInsp3_Y.BackColor = AixsHomeStatus(Kernel.motion.Insp3_Y.AxisMotionIO.SVON == 1, Kernel.motion.Insp3_Y.IsHomeAlready, Kernel.motion.Insp3_Y.axis.State == Advantech.Motion.AxisState.STA_AX_HOMING);
            btnInsp4_Y.BackColor = AixsHomeStatus(Kernel.motion.Insp4_Y.AxisMotionIO.SVON == 1, Kernel.motion.Insp4_Y.IsHomeAlready, Kernel.motion.Insp4_Y.axis.State == Advantech.Motion.AxisState.STA_AX_HOMING);
            //btnInsp1_RM_Z.BackColor = AixsHomeStatus(Kernel.motion.Insp1_RM_Z.AxisMotionIO.SVON == 1, Kernel.motion.Insp1_RM_Z.IsHomeAlready, Kernel.motion.Insp1_RM_Z.axis.State == Advantech.Motion.AxisState.STA_AX_HOMING);
            //btnInsp2_RM_Z.BackColor = AixsHomeStatus(Kernel.motion.Insp2_RM_Z.AxisMotionIO.SVON == 1, Kernel.motion.Insp2_RM_Z.IsHomeAlready, Kernel.motion.Insp2_RM_Z.axis.State == Advantech.Motion.AxisState.STA_AX_HOMING);
            //btnInsp3_RM_Z.BackColor = AixsHomeStatus(Kernel.motion.Insp3_RM_Z.AxisMotionIO.SVON == 1, Kernel.motion.Insp3_RM_Z.IsHomeAlready, Kernel.motion.Insp3_RM_Z.axis.State == Advantech.Motion.AxisState.STA_AX_HOMING);
            //btnInsp4_RM_Z.BackColor = AixsHomeStatus(Kernel.motion.Insp4_RM_Z.AxisMotionIO.SVON == 1, Kernel.motion.Insp4_RM_Z.IsHomeAlready, Kernel.motion.Insp4_RM_Z.axis.State == Advantech.Motion.AxisState.STA_AX_HOMING);

            //lblLDTR1_CurrPos.Text = (Kernel.motion.LDTR_1_Clamp.CurrentActPos / 100.0).ToString();
            //lblLDTR1_CurrForce.Text = Kernel.motion.LDTR_1_Clamp.CurrentActForce.ToString();
            //lblLDTR2_CurrPos.Text = (Kernel.motion.LDTR_2_Clamp.CurrentActPos / 100.0).ToString();
            //lblLDTR2_CurrForce.Text = Kernel.motion.LDTR_2_Clamp.CurrentActForce.ToString();

            //lblULDTR1_CurrPos.Text = (Kernel.motion.ULDTR_1_Clamp.CurrentActPos / 100.0).ToString();
            //lblULDTR1_CurrForce.Text = Kernel.motion.ULDTR_1_Clamp.CurrentActForce.ToString();
            //lblULDTR2_CurrPos.Text = (Kernel.motion.ULDTR_2_Clamp.CurrentActPos / 100.0).ToString();
            //lblULDTR2_CurrForce.Text = Kernel.motion.ULDTR_2_Clamp.CurrentActForce.ToString();

        }
        private Color AixsHomeStatus(bool ServoOn, bool HomeReady, bool Homing)
        {
            Color myColor;
            if (!ServoOn)
            {
                myColor = Color.Red;
            }
            else
            {
                if (HomeReady)
                {
                    myColor = Color.Green;
                }
                else if (!HomeReady && Homing)
                {
                    myColor = PulseOn.Pulse_1s ? Color.Green : Color.White;
                }
                else
                {
                    myColor = Color.White;
                }
            }
            return myColor;
        }

    }
}
